package com.fan.springboot.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.fan.springboot.domain.User;

/**
 * Created by fanjun on 2017/8/14.
 */
@Repository
public class UserDao {

	@Autowired
	private MongoTemplate mongoTemplate;

	public void save(User user) {
		mongoTemplate.save(user);
	}

	// 通过用户名查找用户
	public User findUserByUsername(String username) {
		return mongoTemplate.findOne(new Query(Criteria.where("username").is(username)), User.class);
	}
	
	//查询用户表用户数量
	public  long countUser() {
		Query query = new Query();
        return mongoTemplate.count(query, User.class);
	}
	
	public void updatePassword(User user) {
		Query query = new Query(Criteria.where("username").is(user.getUsername()));
		Update update = new Update().set("password", user.getPassword());
		mongoTemplate.updateFirst(query, update, User.class);
	}
}
